package libcore.net.interceptor;

import java.io.IOException;
import libcore.net.upload.ProgressListener;
import libcore.net.upload.ProgressRequestBody;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 创建时间：2016年10月12日10:55 <br>
 * 作者：jaaksi <br>
 * 描述：上传Interceptor
 */
public class UploadInterceptor implements Interceptor {
  private ProgressListener progressListener;

  public UploadInterceptor(ProgressListener progressListener) {
    this.progressListener = progressListener;
  }

  @Override
  public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();

    if (request.body() == null) {
      return chain.proceed(request);
    }

    Request progressRequest = request.newBuilder()
      .method(request.method(), new ProgressRequestBody(request.body(), progressListener))
      .build();

    return chain.proceed(progressRequest);
  }
}
